﻿using HRMSystemShi.Model;
using Microsoft.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace HRMSystemShi.DAL
{
    public class OperatorService
    {
        public Operator?  GetOperator(string un)
        {
            Operator? op = null;
                string sql = "SELECT * FROM Operator WHERE UserName=@UserName";
                SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@UserName", un));
                if (reader.Read())
                {
                    op = new Operator()
                    {
                        Id = (Guid)reader["id"],
                        UserName = un,
                        Password = reader["Password"].ToString(),
                        IsDeleted = (bool)reader["IsDeleted"],
                        RealName = reader["RealName"].ToString(),
                        IsLocked = (bool)reader["IsLocked"],
                        IsAdmin = (bool)reader["IsAdmin"]
                    };
                }
            return op;
        }
        public static bool PasswordMatch(string password)
        {
            string sql = "SELECT * FROM Operator WHERE Password=@Password";
            return SqlHelper.ExecuteScalar(sql, new SqlParameter("@Password", password)) != null;
        }
        public static Operator GetOperator(Guid id)
        {
            Operator op = new Operator();
            string sql = "SELECT * FROM Operator WHERE Id=@Id";
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@Id", id));
            if (reader.Read())
            {
                op = new Operator
                {
                    Id = id,
                    UserName = reader["UserName"].ToString(),
                    Password = reader["Password"].ToString(),
                    IsDeleted = (bool)reader["IsDeleted"],
                    RealName = reader["RealName"].ToString(),
                    IsLocked = (bool)reader["IsLocked"],
                    IsAdmin = (bool)reader["IsAdmin"]
                };
            }
            return op;
        }
        public static DataTable GetOperatorList()
        {
            string sql = "SELECT Id as ID,UserName as 用户名,RealName as 真实姓名,IsDeleted as 被删除,IsLocked as 被冻结,IsAdmin as 管理员 FROM Operator";
            return SqlHelper.GetDataTable(sql);
        }
        public static bool AddOperator(Operator op)
        {
            string sql = "INSERT INTO Operator VALUES(@Id,@UserName,@Password,@IsDeleted,@RealName,@IsLocked,@IsAdmin)";
            return SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Id", op.Id), new SqlParameter("@UserName", op.UserName), new SqlParameter("@Password", op.Password), new SqlParameter("@IsDeleted", op.IsDeleted), new SqlParameter("@RealName", op.RealName), new SqlParameter("@IsLocked", op.IsLocked), new SqlParameter("@IsAdmin", op.IsAdmin)) > 0;
        }
        public static bool UpdateOperator(Operator op)
        {
            string sql = "UPDATE Operator SET UserName=@UserName,IsDeleted=@IsDeleted,RealName=@RealName,IsLocked=@IsLocked,IsAdmin=@IsAdmin WHERE Id=@Id";
            return SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Id", op.Id), new SqlParameter("@UserName", op.UserName), new SqlParameter("@IsDeleted", op.IsDeleted), new SqlParameter("@RealName", op.RealName), new SqlParameter("@IsLocked", op.IsLocked), new SqlParameter("@IsAdmin", op.IsAdmin)) > 0;
        }
        public static bool UpdatePassword(string userName, string newPassword)
        {
                string sql = "UPDATE Operator SET Password=@Password WHERE UserName=@UserName";
                return SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@Password", newPassword), new SqlParameter("@UserName", userName))>0;
        }
    }
}
